package cqueue.posting;

import java.util.List;

import tcp.unicast.TCPClient;
import tcp.unicast.TCPObserver;
import tcp.unicast.TCPServer;
import cqueue.ListenerEvent;

/**
 * This is the node-end of the marshaling process.
 * 
 * @author Kristopher L. Kalish, James Madison University
 *
 */
public class TcpPostServiceSkel implements PostService<ListenerEvent>, TCPObserver
{
	private List<ListenerEvent> events;
	private TCPClient           tcpclient;
	private Object              lock;

	
	public TcpPostServiceSkel(List<ListenerEvent> events, Object lock, String host, int port)
	{
		this.events = events; // keep a reference of the EventQueue's list.
		this.lock  = lock;
		//tcpclient = new TCPClient("10.0.0.160", 2010);
		
		//TODO: these params should be passed into the constructor, just super convenient this way
		tcpclient = new TCPClient(host, port);
		tcpclient.addObserver(this);
	}
	
	public void postEvent(ListenerEvent event)
	{
		//events.add(event);
	}
	
	public void handleObject(Object o)
	{
		synchronized(lock)
		{
			System.out.println("Posting event " + o + " from tcp.");
			events.add((ListenerEvent) o);
			lock.notifyAll();
		}
	}
}
